﻿using MesErreurs;
using Metier;
using Persistance;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Gestion_commerciale
{
    public partial class FListeCdes : Form
    {
        public FListeCdes()
        {
            InitializeComponent();
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            lvcdes.Items.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("Numéro", 100,HorizontalAlignment.Left);
            lvcdes.Columns.Add("Numéro Vendeur", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Numéro Client", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Date Commande", 100, HorizontalAlignment.Left);
            lvcdes.Columns.Add("Facture", 100, HorizontalAlignment.Left);
            lvcdes.FullRowSelect = true;
        }

        private void AfficherListe()
        {
            Commande unecommande = new Commande();
            List<Commande> mesCommandes;
            string numCde, numVend, NumCli, facture, datecde;
            ListViewItem lvitem_cde;

            lvcdes.Items.Clear();
            lvcdes.Columns.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("1", "Numéro");
            lvcdes.Columns.Add("2", "Numéro Vendeur");
            lvcdes.Columns.Add("3", "Numéro Client");
            lvcdes.Columns.Add("4", "Date Commande");
            lvcdes.Columns.Add("5", "Facture");
            try
            {
                mesCommandes = unecommande.getLesCommandes();

                foreach (Commande c in mesCommandes)
                {

                    numCde = c.NoCommande;
                    // On récupère la property NoVendeur
                    numVend = c.NoVendeur.NoVendeur;
                    NumCli = c.NoClient.NoClient;
                    datecde = c.DateCommande;
                    facture = c.Facture;
                    lvitem_cde = new ListViewItem(new string[] { numCde, numVend, NumCli, datecde, facture }, -1, Color.Black, Color.LightGreen, null);
                    lvcdes.Items.Add(lvitem_cde);
                }

                if (!Numcheck.Checked) lvcdes.Columns.RemoveByKey("1");
                if (!NumVencheck.Checked) lvcdes.Columns.RemoveByKey("2");
                if (!NumClicheck.Checked) lvcdes.Columns.RemoveByKey("3");
                if (!Datecheck.Checked) lvcdes.Columns.RemoveByKey("4");
                if (!Facturecheck.Checked) lvcdes.Columns.RemoveByKey("5");

                lvcdes.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        private void FListeCdes_Load(object sender, EventArgs e)
        {
            this.AfficherListe();
        }

        private void btAjouter_Click(object sender, EventArgs e)
        {
            FInser_Cde finser = new FInser_Cde();
            finser.ShowDialog();
            AfficherListe();
        }

        private void btDetail_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                FDetail_Cde fDetail = new FDetail_Cde(lvcdes.SelectedItems[0].SubItems[0].Text);
                fDetail.ShowDialog();
            }
            else MessageBox.Show("Sélectionner une commande");
        }

        private void btModifier_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                FModif_Cde fModif = new FModif_Cde(lvcdes.SelectedItems[0].SubItems[0].Text);
                fModif.ShowDialog();
                AfficherListe();
            }
            else MessageBox.Show("Sélectionner une commande");
        }

        private void btSupprimer_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                var confirmation = MessageBox.Show("Voulez vous vraiment supprimer la commande ?","Suppression", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (confirmation == DialogResult.Yes)
                {
                    Commande commande =new Commande ();
                    commande=commande.RechercheUneCommande(lvcdes.SelectedItems[0].SubItems[0].Text);
                    commande.supprimerCommande();
                    AfficherListe();
                }
            }
            else MessageBox.Show("Sélectionner une commande");
        }
    }
}
